package com.zrf.gulimall.product.web;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.zrf.gulimall.product.service.SkuInfoService;
import com.zrf.gulimall.product.vo.web.SkuItemVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.util.concurrent.*;

/**
 * @Author zhanrf
 * @Date 2021/12/26 21:56
 */
@Controller
public class ItemController {
	@Resource
	private SkuInfoService skuInfoService;

	@GetMapping("/{skuId}.html")
	public String skuInfo(@PathVariable("skuId") String skuId, Model model) throws ExecutionException, InterruptedException {
		SkuItemVo skuItemVo = skuInfoService.itemInfo(skuId);
		model.addAttribute("item", skuItemVo);
		return "item";
	}

	public static void main(String[] args) {
		ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 5,
				10, TimeUnit.MINUTES,
				new LinkedBlockingDeque<>(5), Executors.defaultThreadFactory(),
				new ThreadPoolExecutor.AbortPolicy());
		Log log = LogFactory.create().createLog(ItemController.class);
		for (int i = 0; i < 11; i++) {
			threadPoolExecutor.execute(() -> {
				try {
					log.info(Thread.currentThread().getId() + "线程开始执行任务");
					Thread.sleep(3000);
					log.info(Thread.currentThread().getId() + "线程结束执行任务");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			});
		}
	}
}
